<?php
/**
 * Created by PhpStorm.
 * User: zd
 * Date: 2018/10/11
 * Time: 10:24
 */

namespace app\api\model;


use traits\model\SoftDelete;

class Work extends BaseModel
{
    protected  $hidden=['create_time'];
    protected $autoWriteTimestamp = true;

    use SoftDelete;
    protected $deleteTime = 'delete_time';




    /**
     * 与work_department表一对多关系
     * @return \think\model\relation\HasOne
     */
    public function contact(){
        return $this->hasOne('ThirdApp','w_id','id');
    }
    /**
     * 与workagent一对多关联
     * @return \think\model\relation\HasMany
     */
    public function agents(){
        return $this->hasMany('WorkAgent','w_id','id');
    }
    /**
     * 创建授权企业信息
     * @param $uid
     * @param $PermanentCode
     * @return $this
     */
    public static function creatWork($uid,$PermanentCode,$type){
        $authInfo = [
            't_id'=>$uid,
            'corpid'=>$PermanentCode->auth_corp_info->corpid,
            'corp_name'=>$PermanentCode->auth_corp_info->corp_name,
            'corp_type'=>$PermanentCode->auth_corp_info->corp_type,
            'corp_square_logo_url'=>$PermanentCode->auth_corp_info->corp_square_logo_url,
            'corp_user_max'=>$PermanentCode->auth_corp_info->corp_user_max,
            'corp_full_name'=>$PermanentCode->auth_corp_info->corp_full_name,
            'subject_type'=>$PermanentCode->auth_corp_info->subject_type,
            'verified_end_time'=>$PermanentCode->auth_corp_info->verified_end_time,
            'corp_wxqrcode'=>$PermanentCode->auth_corp_info->corp_wxqrcode,
            'user_id'=>$PermanentCode->auth_user_info->userid,
            'name'=>$PermanentCode->auth_user_info->name,
            'avatar'=>$PermanentCode->auth_user_info->avatar,
        ];
        $info =self::where('corpid','=',$authInfo['corpid'])->find();//查询已授权信息
        if ($PermanentCode->auth_info->agent == null){//通讯录授权
            $result = self::contactAuth($uid, $PermanentCode, $type, $authInfo, $info, $data='');
        }else{
            $result = self::AIAndBossAuth( $PermanentCode, $type, $authInfo, $info);
        }
        return $result;
    }

    /**
     * 获取用户应用授权信息
     * @param $id
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public static  function getAgent($id){
        return self::where('id','=',$id)
            ->with(['agents'])
            ->find();
    }

    /**
     * 获取应用信息
     * @param $id
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public static  function getAgentInfo($id){
        return self::where('t_id','=',$id)
            ->with(['agents'])
            ->find();
    }

    /**
     * 获取授权信息
     * @param $corpId
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public static function getCountInfo($corpId){
        return self::where('corpid','=',$corpId)
            ->find();
    }

    /**
     * 更新
     * @param $uid
     * @param $type
     * @param $PermanentCode
     * @param $info
     * @return int
     */
    private static function insertAgent($type,$PermanentCode, $info)
    {
        $condition['id']=$info->id;
        $work = self::get($condition);
        $auth_info = $PermanentCode->auth_info->agent;
        foreach ($auth_info as $key=>$value){
            $data[$key]['type'] = $type['type'];
            $data[$key]['suitID'] = $type['suitID'];
            $data[$key]['agentid'] = $value->agentid;
            $data[$key]['name'] = $value->name;
            $data[$key]['round_logo_url'] = $value->round_logo_url;
            $data[$key]['square_logo_url'] = $value->square_logo_url;
            $data[$key]['privilege'] = json_encode($value->privilege);
        }
        $result =  $work->agents()->saveAll($data);
        return $result;
    }

    /**
     * 通讯录授权
     * @param $uid
     * @param $PermanentCode
     * @param $type
     * @param $authInfo
     * @param $result
     * @param $data
     * @return $this
     */
    private static function contactAuth($uid, $PermanentCode, $type, $authInfo, $info, $data): Work
    {
        $authInfo['permanent_code'] = $PermanentCode->permanent_code;//联系人永久授权码
        if (empty($info)) {//添加授权记录
            $result = self::create($authInfo);
            ThirdApp::where('id', '=', $uid)->update(['w_id' => $result->id]);
        } else {//更新授权记录
            self::where('corpid', '=', $authInfo['corpid'])->update($authInfo);
        }
        $data['type'] = $type['type'];
        $data['suitID'] = $type['suitID'];
        $result->agents()->save($data);
        return $result;//数据入库
    }

    /**
     * crm和人事授权
     * @param $uid
     * @param $PermanentCode
     * @param $type
     * @param $authInfo
     * @param $result
     * @return mixed
     */
    public static function AIAndBossAuth( $PermanentCode, $type, $authInfo, $info)
    {
        if ($type['type'] == 'ai') {
            $authInfo['permanent_code_ai'] = $PermanentCode->permanent_code;//AI雷达的永久授权码
        } elseif ($type['type'] == 'boss') {
            $authInfo['permanent_code_boss'] = $PermanentCode->permanent_code;//BOSS雷达的永久授权码
        }
        self::where('corpid', '=', $authInfo['corpid'])->update($authInfo);//不是通讯录授权
        $result = self::insertAgent($type, $PermanentCode, $info);
        return $result;
    }
}